﻿using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb.Request
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public abstract class APayRequest : ARequest
    {
        protected readonly ITradeChannel Channel;
        protected readonly IOrderPayApply Order;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        protected APayRequest(ITradeChannel channel,
           IOrderPayApply order,
           GybConfigure config)
            : base(config)
        {
            ExceptionUtils.CheckNotNull(channel, nameof(channel));
            if (order == null)
            {
                channel.ThrowChannelValidationException("请求的 order 为 null");
            }
            if (config == null)
            {
                channel.ThrowChannelValidationException("请求的 config 为 null");
            }
            if (config.ConfigureInfo == null)
            {
                channel.ThrowChannelValidationException("请求的 config 的 ConfigureInfo 为 null");
            }
            this.Channel = channel;
            this.Order = order;
            this.MercDtTm = DateTime.Now.ToString("yyyyMMddHHmmss");
            this.TermSsn = order.OrderNo;
            this.TermCode = config.ConfigureInfo.TermCode;
            if (string.IsNullOrWhiteSpace(this.TermCode))
            {
                this.TermCode = "00000000";
            }
            this.TranAmt = Math.Round(order.OrderMoney, 2);
            this.Ip = "";
            this.SubMercFlag = 1;
            this.SubMercName = config.ConfigureInfo.SubMercName;
            this.SubMercGoodsName = " " + FixedLengthString(order.Body, 40);
            this.Remark1 = FixedLengthString(order.Subject, 11);
            this.Remark2 = order.ExtraParam;
        }

        /// <summary>
        /// 商户的交易日期时间yyyyMMddhhMMss
        /// </summary>
        public string MercDtTm { get; set; }

        /// <summary>
        /// 订单号，不能重复
        /// </summary>
        public string TermSsn { get; set; }

        /// <summary>
        /// 终端号,可以全为0
        /// </summary>
        public string TermCode { get; set; }

        /// <summary>
        /// 交易金额,元为单位，如：11.01(表示11元1分)，11（表示11元）
        /// </summary>
        public decimal TranAmt { get; set; }

        /// <summary>
        /// 交易备注1
        /// </summary>
        public string Remark1 { get; set; }

        /// <summary>
        /// 交易备注2
        /// </summary>
        public string Remark2 { get; set; }

        /// <summary>
        /// 订单生产时的IP地址,如果该项为空，则不进行IP地址检查
        /// </summary>
        public string Ip { get; set; }

        /// <summary>
        /// 二级商户类别,0-消费虚拟类
        ///              1-消费实物类
        ///              2-投资理财类
        ///必输
        /// </summary>
        public int SubMercFlag { get; set; }

        /// <summary>
        /// 二级商户名,必输
        /// </summary>
        public string SubMercName { get; set; }

        /// <summary>
        /// 二级商品名
        /// </summary>
        public string SubMercGoodsName { get; set; }

        /// <summary>
        /// 生成
        /// </summary>
        /// <returns></returns>
        public abstract ITradePayApplyResult Build();
    }
}
